home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / FAXGETE2.C < prev    next >
Text File  |  1990-01-08  |  4KB  |  112 lines

  1.  
  2.  
  3. /*
  4.    FAXGETE2.C  A high-level CAS Toolkit function.
  5.  
  6.    This function gets the Cover page text of an event.
  7.  
  8.    INPUT:  An event handle and possibly a queue number, and possibly a buffer
  9.             to return the cover page text into.
  10.  
  11.    OUTPUT: A pointer to the cover page text.
  12. */
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <malloc.h>
  17. #include <cas.h>
  18. #include <fax.h>
  19.  
  20. char * pascal FAXGetEventCover(int EventHandle,
  21.                                BYTE *queue,
  22.                                char *CoverPtr)
  23. {
  24.   int FileHandle,                 /* returned from CASFind functions */
  25.       retval,                     /* for return value of CAS calls */
  26.       red,                        /* bytes read with the read() */
  27.       CoverSize;
  28.   ECF ControlInfo;                /* need FTROffset to determine coversize */
  29.   char *buffer;                   /* to hold the cover page text */
  30.   CECS CurrentEventInfo;
  31.  
  32.   FAXerrno = CASerrorcode = 0;      /* They keep it if nothing goes wrong */
  33.  
  34.   /* If the event is currently executing, can't get its cover page text */
  35.   if (EventHandle == CASGetCurrentEventStatus(&CurrentEventInfo)) {
  36.     FAXerrno = EVENTISCURRENT;
  37.     return(NULL);
  38.   }
  39.  
  40.   /* If no queue specified, search all the queues for the given Event Handle */
  41.   if (*queue == UNKNOWN_QUEUE) {
  42.     for (*queue = TASK_QUEUE; *queue <= LOG_QUEUE; (*queue)++) {
  43.       retval = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, *queue);
  44.       while (retval && (retval != EventHandle)) {
  45.         if (retval > 0) {           /* found an event, but not the one sought */
  46.           retval = CASFindNext(*queue);    /* so find next one */
  47.         }
  48.         else break;                 /* end of queue, or other error */
  49.       }
  50.       if (retval == EventHandle) break;           /* else go to next queue */
  51.     }                                             /* Done with all the queues */
  52.     if (retval != EventHandle) {       /* after all that, just give up! */
  53.       *queue = UNKNOWN_QUEUE;
  54.       FAXerrno = EVENTNOTFOUND;
  55.       CASerrorcode = -retval;
  56.       return(NULL);
  57.     }
  58.   }
  59.  
  60.   /* To get to here, the queue was given, or we found it anyway */
  61.   retval = CASOpenFile(EventHandle, 0, *queue);
  62.   if (retval < 0) {                            /* CAS error, get out */
  63.     FAXerrno = OPENFILE;
  64.     CASerrorcode = -retval;
  65.     return(NULL);
  66.   }
  67.   FileHandle = retval;
  68.  
  69.   red = read(FileHandle, &ControlInfo, sizeof(ECF));
  70.   if (red != sizeof(ECF)) {
  71.     FAXerrno = CANTREADFILE;
  72.     return(NULL);
  73.   }
  74.  
  75.   /* If the event is a received FAX, the cover text has already been
  76.       integrated into the first fax page; this function can't retrieve it */
  77.   if ((ControlInfo.EventType == RECEIVE) &&
  78.       (ControlInfo.TransferType != FILE_TRANSFER)) {
  79.     FAXerrno = RECEIVEDFAXNOCOVER;
  80.     return(NULL);
  81.   }
  82.  
  83.   /* Otherwise, find out the cover size, and fetch it */
  84.   CoverSize = ControlInfo.FTROffset - 383;
  85.  
  86.   /* Fetch only if cover IS there (if size 1, it's only the terminating NULL) */
  87.   if (!(CoverSize > 1)) {
  88.     FAXerrno = NOCOVERTEXT;
  89.     return(NULL);
  90.   }
  91.  
  92.   if (CoverPtr) {                    /* It had better be big enough */
  93.     buffer = CoverPtr;
  94.   }
  95.   else {
  96.     buffer = (char *)malloc(CoverSize);
  97.     if (!buffer) {
  98.       FAXerrno = OUTOFMEMORY;
  99.       return(NULL);
  100.     }
  101.   }
  102.   red = read(FileHandle, buffer, CoverSize);
  103.   if (red != CoverSize) {
  104.     FAXerrno = CANTREADFILE;
  105.     return(NULL);
  106.   }
  107.   if (close(FileHandle) == -1) {
  108.     FAXerrno = CANTCLOSEFILE;          /* Warning only, we got what we wanted */
  109.   }
  110.   return(buffer);
  111. }
  112.